package com.digitalart.section.index
{
	import flash.events.EventDispatcher;
	import com.digitalart.commons.CustomEvent;
	import com.digitalart.commons.CustomAssetLoader;
	import flash.events.Event;

	public class IndexController extends EventDispatcher
	{
		public static const INDEX_READY:String = "onIndexLoadingReady";
		
		private var _indexModel : IndexModel;
		private var _indexView : IndexView;
		
		/**********************************Constructor*************************/
		
		public function IndexController() {
			
			super(this);
		}
		
		
		/**********************************************************************/
		
		/**********************************Methods*************************/
		
		
		public function init():void {
			 
			_indexModel = new IndexModel();
			_indexModel.addEventListener(IndexModel.ON_INDEX_MODEL_READY, onDataLoaded);
			_indexModel.init();
			
			
		}
		
		private function onDataLoaded(event : CustomEvent) : void {
			_indexModel.removeEventListener(IndexModel.ON_INDEX_MODEL_READY, onDataLoaded);
			
			var vo:IndexVO = event.data as IndexVO;
			
			_indexView = new IndexView();
			_indexView .underConstructionCopy.htmlText = vo.underConstructionCopy;
	  
			
			loadViewAssets(vo);
		}
		
		private function loadViewAssets(vo : IndexVO) : void {
			
			var bulkLoader : CustomAssetLoader = CustomAssetLoader.getInstance();
			
			var paths : Array = new Array();
			 
			paths.push(vo.backgroundPath);
			//paths.push(vo.meshPath);
			//paths.push(vo.carPath);
			
			
			bulkLoader.bulkLoaderPathList = paths;
			bulkLoader.addEventListener(CustomAssetLoader.ON_BULK_LOADER_COMPLETE, onAssetsReady);
			bulkLoader.bulkLoad();
		}
		
		private function onAssetsReady(event : CustomEvent) : void {
			
			event.target.removeEventListener(CustomAssetLoader.ON_BULK_LOADER_COMPLETE, onAssetsReady);
			
			var bitmaps : Array = event.data;
			
			_indexView.background = bitmaps[0];
			_indexView.addEventListener(IndexView.ON_INDEX_VIEW_READY, onViewReady);
			_indexView.init();
			
			
		}
		
		private function onViewReady(event : Event) : void {
			
			_indexView.removeEventListener(IndexView.ON_INDEX_VIEW_READY, onViewReady);
			dispatchEvent(new Event(INDEX_READY));
		}
		
		/**********************************************************************/
		/**************************Getters & Setters***************************/
		
		public function getView() : IndexView {
			return _indexView;
		}
	}
}